package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_dingass;

import cn.com.api.service.ApiT_dingassService;

import cn.com.util.*;
import cn.com.util.*;

public class T_dingassBaseController {

	private static Logger logger = Logger
			.getLogger(T_dingassBaseController.class);

	@Autowired
	private ApiT_dingassService apiT_dingassService;

	/**
	 * 列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_dingass-list")
	public ModelAndView t_dingass_list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_dingass_list.jsp");

		List<ApiT_dingass> t_dingasss = apiT_dingassService.findT_dingasss();
		mv.addObject("t_dingasss", t_dingasss);

		return mv;
	}

	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_dingass-add-pre")
	public ModelAndView t_dingass_add_pre(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_dingass_add.jsp");
		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiT_dingass t_dingass = apiT_dingassService
					.findT_dingassById(Integer.valueOf(id));
			mv.addObject("t_dingass", t_dingass);
		}
		return mv;
	}

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_dingass-add")
	public void t_dingass_add(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String did = request.getParameter("did");
		String rid = request.getParameter("rid");
		String money = request.getParameter("money");
		String num = request.getParameter("num");
		String product_id = request.getParameter("product_id");

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		// add
		if (id == null || (id.trim()).equals("")) {
			ApiT_dingass t_dingass = new ApiT_dingass();

			t_dingass.setDid(did);
			t_dingass.setRid(rid);
			t_dingass.setMoney(Float.valueOf(money));
			t_dingass.setNum(Integer.valueOf(num));
			t_dingass.setProduct_id(product_id);

			apiT_dingassService.insertT_dingass(t_dingass);
		}
		// edit
		else {
			ApiT_dingass t_dingass = apiT_dingassService
					.findT_dingassById(Integer.valueOf(id));

			t_dingass.setDid(did);
			t_dingass.setRid(rid);
			t_dingass.setMoney(Float.valueOf(money));
			t_dingass.setNum(Integer.valueOf(num));
			t_dingass.setProduct_id(product_id);
			apiT_dingassService.updateT_dingass(t_dingass);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_dingass-delete")
	public void t_dingass_delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		// delete
		int ret_i = apiT_dingassService.deleteT_dingass(Integer.valueOf(id));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

}
